package cn.example.vip.mall.page.controller;

import cn.example.vip.mall.page.service.SeckillPageService;
import cn.example.vip.mall.seckill.entity.SeckillGoods;
import cn.example.vip.mall.seckill.feign.SeckillGoodsFeign;
import cn.example.vip.mall.util.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/page")
public class SeckillPageController {
	@Autowired
	private SeckillPageService seckillPageService;
	@Autowired
	private SeckillGoodsFeign seckillGoodsFeign;
	
	/**
	 * 根据 id 生成秒杀商品页面
	 * @param id
	 * @return
	 */
	@GetMapping("/seckill/goods/{id}")
	public RespResult html(@PathVariable("id") String id) throws Exception {
		seckillPageService.html(id);
		return RespResult.ok();
	}
	
	/**
	 * 根据 id 删除秒杀商品页面
	 * @param id
	 * @return
	 */
	@DeleteMapping("/seckill/goods/{id}")
	public RespResult delHtml(@PathVariable("id") String id) {
		seckillPageService.delHtml(id);
		return RespResult.ok();
	}
	
	/**
	 * 根据 activityId 删除秒杀商品页面
	 * @param activityId
	 * @return
	 */
	@DeleteMapping("/seckill/act/{activityId}")
	public RespResult delByActivityId(@PathVariable("activityId") String activityId) {
		// 1.查询相关秒杀商品的列表
		RespResult<List<SeckillGoods>> respResult = seckillGoodsFeign.queryByActId(activityId);
		List<SeckillGoods> seckillGoodsList = respResult.getData();
		// 2.删除商品页面
		if(seckillGoodsList != null && seckillGoodsList.size() > 0) {
			for(SeckillGoods seckillGoods : seckillGoodsList) {
				seckillPageService.delHtml(seckillGoods.getId());
			}
			return RespResult.ok();
		}
		return RespResult.error();
	}
}
